export function add(a, b) {
  return a + b;
}

export function sleep(ms) {
  return new Promise((resolve) => setTimeout(resolve, ms));
}

export function isEmojiCharacter(substring) {
  if (!substring) return;
  for (var i = 0; i < substring.length; i++) { 
    var hs = substring.charCodeAt(i);      
    if (0xd800 <= hs && hs <= 0xdbff) {         
      if (substring.length > 1) {            
        var ls = substring.charCodeAt(i + 1);            
        var uc = ((hs - 0xd800) * 0x400) + (ls - 0xdc00) + 0x10000;            
        if (0x1d000 <= uc && uc <= 0x1f77f) {               
          return true;            
        }
      }
    } else if (substring.length > 1) {         
      var ls = substring.charCodeAt(i + 1);         
      if (ls == 0x20e3) {            
        return true;         
      }
    } else { 
      if (0x2100 <= hs && hs <= 0x27ff) {            
        return true;         
      } else if (0x2B05 <= hs && hs <= 0x2b07) {            
        return true;         
      } else if (0x2934 <= hs && hs <= 0x2935) {            
        return true;         
      } else if (0x3297 <= hs && hs <= 0x3299) {            
        return true;         
      } else if (hs == 0xa9 || hs == 0xae || hs == 0x303d || hs == 0x3030 || hs == 0x2b55 || hs == 0x2b1c || hs == 0x2b1b || hs == 0x2b50) {
        return true;         
      }
    }
  }
}

export function generateUUID() {
  var d = new Date().getTime();
  var uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
    var r = (d + Math.random()*16)%16 | 0;
    d = Math.floor(d/16);
    return (c=='x' ? r : (r&0x3|0x8)).toString(16);
  });
  return uuid;
};

export function datefmt (date, fmt) { //author: meizz
  var o = {
      "M+": date.getMonth() + 1, //月份
      "d+": date.getDate(), //日
      "h+": date.getHours(), //小时
      "m+": date.getMinutes(), //分
      "s+": date.getSeconds(), //秒
      "q+": Math.floor((date.getMonth() + 3) / 3), //季度
      "S": date.getMilliseconds() //毫秒
  };

  if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
  for (var k in o)
  if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));

  return fmt;
}

export function generateCode() {
 var jschars = ['0','1','2','3','4','5','6','7','8','9'];
  var res = "";
  for(var i = 0; i < 4 ; i ++) {
    var id = Math.ceil(Math.random()*9);
    res += jschars[id];
  }
  return res;
}

export function checkPhone(phone){
  return /^1[345678]\d{9}$/.test(phone);
}

export function NewGuid (){
  function S4() {
    return (((1 + Math.random()) * 0x10000) | 0).toString(16).substring(1);
  }
  return (S4() + S4() + "-" + S4() + "-" + S4() + "-" + S4() + "-" + S4() + S4() + S4());
}
